package com.example.protoc2application;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.protobuf.InvalidProtocolBufferException;
import com.sp.StudentOuterClass;

import java.security.ProtectionDomain;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_hello;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_hello = findViewById(R.id.tv_hello);
        findViewById(R.id.btn_dd).setOnClickListener(this);



    }

    @Override
    public void onClick(View v) {
        //对象构造器被私有化
        //通过student的内部类builder构建
//        StudentOuterClass studentOuterClass = new StudentOuterClass();
        StudentOuterClass.Student.Builder builder = StudentOuterClass.Student.newBuilder();
        builder.setName("张飞");
        builder.setLevel(false);
        builder.setAge(17);
        builder.setScore(100);
        builder.setSex(StudentOuterClass.Student.Sex.MALE);

        //封装
        StudentOuterClass.Student student = builder.build();
        //转换成字节数组用于传输,存储即序列化
        byte[] stubyte = student.toByteArray();
        try {
            //将字节数据转换成对对应实例即反序列化
            StudentOuterClass.Student student2 = StudentOuterClass.Student.parseFrom(stubyte);
            tv_hello.setText(student2.getName());
        } catch (InvalidProtocolBufferException e) {
            throw new RuntimeException(e);
        }
    }
}